package com.sihan.framework.etm.controller.baseData;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.framework.annotation.BusinessLog;
import com.framework.model.ApiResult;
import com.sihan.framework.etm.dto.baseData.ToolDTO;
import com.sihan.framework.etm.service.baseData.ToolService;
import com.sihan.framework.etm.vo.baseData.ToolVO;
import com.sihan.framework.etm.vo.business.ToolOverviewVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "工器具管理")
@RestController
@RequestMapping("/system/v0/tool")
public class ToolController {

	@Autowired
	private ToolService toolService;
	  
	@ApiOperation(value = "工器具列表", notes = "工器具列表", response = ToolVO.class)
	@GetMapping("/list")
	@BusinessLog(value = "工器具列表")
	@ApiImplicitParams({
	      @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
	      @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
	      @ApiImplicitParam(name = "orderBy", value = "排序字段", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "code", value = "工器具编号", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "deviceCode", value = "设备编号", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "isBind", value = "是否绑定（-1:全部，0：未绑定，1：绑定）", dataType = "int", paramType = "query"),
	      @ApiImplicitParam(name = "queryStartTime", value = "查询开始检", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "queryEndTime", value = "查询结束时间", dataType = "string", paramType = "query")
	  })
	@RequiresAuthentication
	public ApiResult<?> listTool(ToolDTO tool){
		if(null != tool && null == tool.getIsBind()) {
			tool.setIsBind(-1);
		}
		return ApiResult.success(toolService.selectPage(tool));
	}
	
	@ApiOperation("绑定与解绑")
	@GetMapping("/bind_unbind_device")
	@BusinessLog(value = "绑定与解绑")
	public ApiResult<?> bingOrUnBindDevice(String id,String positionDeviceId,Integer type) {
		toolService.bindOrUnbindDevice(id, positionDeviceId, type);
		return ApiResult.success();
	}
	
	@ApiOperation("工器具信息")
	@GetMapping("/detail")
	@BusinessLog(value = "工器具信息")
	public ApiResult<?> findById(String id) {
		return ApiResult.success(toolService.findById(id));
	}
	
	@ApiOperation("修改工器具")
	@PostMapping("/update")
	@BusinessLog(value = "修改工器具")
	public ApiResult<?> editTool(@Valid @ApiParam(name = "toolDTO", value = "工器具数据", required = true) 
		@RequestBody ToolOverviewVO dto) {
		toolService.update(dto);
		return ApiResult.success();
	}
	
	@ApiOperation("台账与电子标签解绑")
	@PostMapping("/rfid/unbind")
	@BusinessLog(value = "台账与电子标签解绑")
	public ApiResult<?> rfidUnbind(@RequestParam String id) {
		return toolService.rfidUnbind(id);
	}
}
